﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ProductionManage.Models.DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using wangyaxin.Jiangzhenying.UnitInfos.Dto;
using wangyaxin.Jiangzhenying.Workstations;
using wangyaxin.Jiangzhenying.WorkstationWorkers;
using wangyaxin.Jiangzhenying.WorkstationWorkers.Dto;
using wangyaxin.Models;
using wangyaxin.Wang.Dto;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace wangyaxin.Jiangzhenying
{
    [ApiExplorerSettings(GroupName = "江珍莹")]
    public class WorkstationWorkerService : ApplicationService, IWorkstationWorkerIService
    {
        private readonly IRepository<WorkstationWorker, int> _repository;
        public WorkstationWorkerService(IRepository<WorkstationWorker, int> repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 获取人力资源信息
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<string> GetWorkstationWorkers()
        {
            var works = await _repository.GetListAsync();
            var worksDto = ObjectMapper.Map<List<WorkstationWorker>, List<WorkstationWorkerDto>>(works);
            return JsonConvert.SerializeObject(new PageDto<WorkstationWorkerDto>
            {
                code = "", // 可以根据需要设置状态码
                data = worksDto, // 返回添加的用户信息
                msg = "获取成功", // 可以根据需要添加消息
                count = works.Count // 添加的用户数量  
            });
        }

        /// <summary>
        /// 添加人力资源信息
        /// </summary>
        /// <returns></returns>
        public async Task<WorkstationWorker> AddWorkstationWorkers(WorkstationWorker works)
        {
            var addUnitInfo = ObjectMapper.Map<WorkstationWorker, WorkstationWorker>(works);
            await _repository.InsertAsync(addUnitInfo);
            return addUnitInfo;
        }

        /// <summary>
        /// 删除人力资源信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [RemoteService(false)]
        public async Task<string> DeleteWorkstationWorkers(int id)
        {
            var workstationWorker = await _repository.GetAsync(x => x.Id == id);
            await _repository.DeleteAsync(workstationWorker, true);
            return JsonConvert.SerializeObject(new PageDto<UnitInfoDto> { code = "200", data = null, msg = "成功", count = 1 });
        }

        /// <summary>
        /// 反填人力资源信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<WorkstationWorker> GetWorkstationWorkerById(int id)
        {
            var works = await _repository.GetAsync(id);
            return ObjectMapper.Map<WorkstationWorker, WorkstationWorker>(works);
        }

        /// <summary>
        /// 更新人力资源信息
        /// </summary>
        /// <param name="workstationWorker"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task UpdateWorkstationWorkers(int id, WorkstationWorker works)
        {
            var wang = await _repository.GetAsync(id);
            wang.PostName = works.PostName;
            wang.Quantity = works.Quantity;
            wang.CreateBy = works.CreateBy;
            wang.CreateTime = works.CreateTime;
            wang.UpdateBy = "孙宝硕";
            wang.UpdateTime = DateTime.Now;
            await _repository.UpdateAsync(wang);
        }
    }
}
    
